home *** CD-ROM | disk | FTP | other *** search
- #Editor Script to fix indentation.
- # (c) Solutionsoft, 1998
- # Revised to handle comments correctly and to indent HTML
- # code embedded inside a Perl script.
- # John Weaver, 2000 v1.3 mailto:Perl@jweaver.net
-
- #To use:
- #Choose Edit | Select ALL then run this editor script.
-
- # The current line is in $_.
- $reply = $_;
- chomp ($reply); # remove trailing newline
- $reply =~ s/^ *//; # remove leading spaces
- $reply =~ s/^\t*//; # remove leading tabs
- if (!$inhtml) { # if not inside html
- if ($reply =~ /^}.*/) { # match curly brace at begin of line
- $level--;
- }
- if ($reply =~ /^sub\b/) { # match sub at begin of line
- $level = 0;
- }
- if ($reply =~ /^else\b/) { # match else at begin of line
- $level = $prevlevel;
- }
- if ($reply =~ /^elsif\b/) { # match elsif at begin of line
- $level = $prevlevel;
- }
- if ($reply =~ /^print *<<(.*);/i) { # match print at begin of line
- $ENDPRINT = $1;
- $PREVLEVEL = $level;
- $inhtml = 1;
- }
- } else { # only if inside html code
- # ---------------------------------------------------------
- my $temp = $ENDPRINT;
- $temp =~ s/^ *//; # remove leading spaces
- if ($reply =~ /^$temp/) {
- $level = 0;
- $reply = $ENDPRINT;
- $inhtml = 0;
- $ENDPRINT = '';
- } else {
- if (htmlscan('first', $reply) == -1) {
- $level--;
- }
- }
- }
- # ---------------------------------------------------------
- $prefix = "" ;
- foreach $i (1 .. $level){$prefix = $prefix." ";} # concat leading whitespace
- #print the result,
- #which will replace the selected text in the IDE.
- print "$prefix$reply\n"; # output line
- $prevlevel = $level;
- if (!$inhtml) { # if not inside html
- if ($PREVLEVEL) {
- $level = $PREVLEVEL;
- $PREVLEVEL = 0;
- }
- if (!($reply =~ /^\#/)) { # ignore comment lines
- if ($reply =~ /^.{1,}{ *?$/) { # find brace at end of line
- $level++;
- }
- if ($reply =~ /^.{1,}{ *\#/) { # find eol brace followed by comments
- $level++;
- }
- if ($reply =~ /^.{1,}} *?$/) { # find brace at end of line
- $level--;
- }
- if ($reply =~ /^.{1,}} *\#/) { # find eol brace followed by comments
- $level--;
- }
- }
- } else {
- if (htmlscan('first', $reply) == 1) {
- $level++;
- }
- if (htmlscan('last', $reply) == -1) {
- $level--;
- }
- }
- if ($level < 0) {$level = 0} ;
-
- sub htmlscan {
- my ($mode, $line) = @_;
- my @tags = qw(<P <TR <TD <TABLE <BODY
- </P </TR </TD </TABLE </BODY );
- my $found;
- my $tag;
- $mode =~ tr/a-z/A-Z/; # uppercase mode
- if ($mode eq 'FIRST') { # first tag in line
- $line =~ /.*?(<.*?)[ >]/;
- $found = $1;
- } else { # last tag in line
- $line =~ /.{3,}(<.*)[ >].*$/;
- $found = $1;
- }
- $found =~ tr/a-z/A-Z/; # uppercase tag found
- if ($found) { # if tag found in line
- foreach $tag (@tags) { # scan table
- if ($found eq $tag) { # is tag in table
- if ($tag =~ /^<\//) { # does tag have slash
- return -1; # if end tag
- } else {
- return 1; # if start tag
- }
- }
- }
- }
- return 0; # if tag not matched
- }
-
-